/**
 * @author: Loy-winters1224@163.com
 * @date: 2014-8-20-下午5:29:04
 */
package net.loyintean.pearlshell.sm.web;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor;

/**
 * an interceptor to log exception infos.
 * <p>
 * if you configure the global-exception-mappings in struts.xml, then the
 * exception infomation won't be loged.
 * <p>
 * so I need an self-defining interceptor to record the exception info.
 * <p>
 * use it like this:<br>
 * {@code<interceptors>} <br>
 * {@code <interceptor name="exceptionLogger"
				class="net.loyintean.pearlshell.sm.web.ExceptionLogInterceptor" />} <br>
 * {@code<interceptor-stack name="myStack">} <br>
 * {@code	<interceptor-ref name="defaultStack" />} <br>
 * {@code	<interceptor-ref name="exceptionLogger" />} <br>
 * {@code</interceptor-stack>} <br>
 * {@code</interceptors>} <br>
 * {@code	<default-interceptor-ref name="myStack" />} <br>
 * 
 * <p>
 * Loy: add field of {@link #devMode}, and use it to control the printstack().
 * v2014-8-21.1.1.0
 * 
 * @author Loy-winters1224@163.com
 * @version 2014-8-21.1.1.0
 */
public class ExceptionLogInterceptor extends ExceptionMappingInterceptor {

	/**
	 *
	 */
	private static final Logger LOGGER = Logger
			.getLogger(ExceptionLogInterceptor.class);

	/**
	 *
	 */
	private static final long serialVersionUID = -7303039456355407922L;

	/**
	 * the struts constant "struts.devMode". if it's true, the
	 * {@link #handleLogging(Exception)} will print Exception's stack info.
	 * <p>
	 * the default vaule is true.
	 * 
	 */
	private boolean devMode = true;

	/**
	 * set the logEnabled be true by default.
	 * 
	 * @author Loy-winters1224@163.com
	 */
	public ExceptionLogInterceptor() {
		logEnabled = true;
	}

	/**
	 * record the exception info by {@link #LOGGER}, while print it's stack
	 * directly.
	 * 
	 * @author Loy-winters1224@163.com
	 * @see com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor
	 *      #handleLogging(java.lang.Exception)
	 * 
	 */
	@Override
	protected void handleLogging(Exception e) {
		LOGGER.error(e);
		if (devMode) {
			e.printStackTrace();
		}
	}

	/**
	 * @param devMode
	 *            the {@link #devMode }to set
	 */
	@Inject(value = "struts.devMode")
	public void setDevMode(String devMode) {
		this.devMode = Boolean.valueOf(devMode);
	}
}
